package com.lingyun.financialmanagement.service.pojo;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lingyun.financialmanagement.dao.mapper.UsersMapper;
import com.lingyun.financialmanagement.dao.pojo.Users;
import com.lingyun.financialmanagement.service.UserService;
import com.lingyun.financialmanagement.utils.TokenGetUser;
import com.lingyun.financialmanagement.vo.ErrorCode;
import com.lingyun.financialmanagement.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${redis.token.tokenAccountPrefix}")
    private String tokenAccountPrefix;

    @Override
    public Result selectUserById(Integer id) {
        Users users = usersMapper.selectById(id);
        return Result.success(users);
    }

    @Override
    public Result updateUserByToken(Users users) {
        if (users == null){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PASSWORD_ERROR.getMsg());
        }
        Result usersResult = TokenGetUser.getUsers(request, redisTemplate, tokenAccountPrefix, usersMapper);
        if (usersResult.getCode() != 200){
            return Result.success(usersResult);
        }
        Users usersSelect = (Users) usersResult.getData();

        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        if (users.getName() != null && !users.getName().isEmpty()){
            updateWrapper.set("name", users.getName());
        }
        if (users.getSex() != null && !users.getSex().isEmpty()){
            updateWrapper.set("sex", users.getSex());
        }
        if (users.getPhone()!= null && !users.getPhone().isEmpty()){
            updateWrapper.set("phone", users.getPhone());
        }
        updateWrapper.eq("id", usersSelect.getId());

        int update = 0;
        try{
            update = usersMapper.update(updateWrapper);
        } catch (Exception e){
            return Result.fail(ErrorCode.UPDATE_ERROR.getCode(), ErrorCode.UPDATE_ERROR.getMsg());
        }
        if (update == 0){
            return Result.fail(ErrorCode.UPDATE_ERROR.getCode(), ErrorCode.UPDATE_ERROR.getMsg());
        }
        return Result.success(usersResult);
    }

    @Override
    public Result updatePasswordByToken(String oldPassword, String newPassword) {
        Result usersResult = TokenGetUser.getUsers(request, redisTemplate, tokenAccountPrefix, usersMapper);
        if (usersResult.getCode() != 200){
            return usersResult;
        }
        Users users = (Users) usersResult.getData();
        // 验证密码
        String oldPasswordMd5 = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!users.getPassword().equals(oldPasswordMd5)){
            return Result.fail(ErrorCode.PASSWORD_ERROR.getCode(), ErrorCode.PASSWORD_ERROR.getMsg());
        }

        // 新的密码
        String newPasswordMd5 = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        users.setPassword(newPasswordMd5);
        // 更新
        int update = 0;
        try{
            update = usersMapper.updateById(users);
        } catch (Exception e){
            return Result.fail(ErrorCode.UPDATE_ERROR.getCode(), ErrorCode.UPDATE_ERROR.getMsg());
        }
        if (update == 0){
            return Result.fail(ErrorCode.UPDATE_ERROR.getCode(), ErrorCode.UPDATE_ERROR.getMsg());
        }
        return Result.success(users);
    }
}
